package sketch.skeleton;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.jdt.core.dom.ImportDeclaration;

import sketch.util.Checker;
import sketch.util.Globals;


public class Skeleton {
	
	public final List<String> importsList = new LinkedList<String>();
	
	/**
	 * A skeleton is composed by a bunch of Java source lines and generators
	 * */
	public final List<SkeletonElement> elements = new ArrayList<SkeletonElement>();
	
	//this is for fast look up
	public final Map<Integer, SkeletonElement> generatedHoles = new LinkedHashMap<Integer, SkeletonElement>();
	
	public void addElement(SkeletonElement element) {
		Checker.checkNull(element, "The newly added element could not be null!");
		this.elements.add(element);
	}
	
	public void setGeneratedHoles(int i, SkeletonElement element) {
		Checker.checkTrue(element instanceof GeneratorSourceCode, "Wrong type: " + element);
		Checker.checkTrue(!generatedHoles.containsKey(i), "The position: " + i + " should not contain!");
		this.generatedHoles.put(i, element);
	}
	
	
	public void addImports(List<ImportDeclaration> imports) {
		Checker.checkNull(imports, "The import list could not be null.");
		for(ImportDeclaration importStmt : imports) {
			importsList.add(importStmt.toString());
		}
	}
	
	public String importString() {
		StringBuilder sb = new StringBuilder();
		for(String str : importsList) {
			sb.append(str.trim());
			sb.append(Globals.lineSep);
		}
		return sb.toString();
	}
	
	public String toCodeString() {
		StringBuilder sb = new StringBuilder();
		for(SkeletonElement element : elements) {
			sb.append(element.toCodeString());
			sb.append(Globals.lineSep);
		}
		return sb.toString();
	}
	
	@Override
	public String toString() {
		return this.toCodeString();
	}
}